/**
 * 套餐类
 *
 * @project sky-take-out2
 * @author MECHREVO
 * @date 2023/12/2 17:48:32
 * @version 1.0
 */
package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetMealDishMapper setMealDishMapper;

    @Autowired
    private DishMapper dishMapper;
    /**
     * 添加套餐
     *
     * @param setmealDTO
     */
    @Override
    @Transactional
    @CacheEvict(cacheNames = "SETMEAL", allEntries = true)
    public void addSetMeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.insert(setmeal);
        Long setmealId = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealId);
        });
        setMealDishMapper.insertBatch(setmealDishes);
    }

    /**
     * 分页查询套餐
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        List<SetmealVO> list = setmealMapper.page(setmealPageQueryDTO);
        Page<SetmealVO> p = (Page<SetmealVO>) list;
        PageResult pageResult = new PageResult(p.getTotal(), p.getResult());
        return pageResult;
    }

    @Transactional
    @Override
    @CacheEvict(cacheNames = "SETMEAL", allEntries = true)
    public void DeleteSetMeal(List<Long> ids) {
        for (Long id : ids) {
            SetmealVO setmealvo=setmealMapper.getById(id);
            if (setmealvo.getStatus() == StatusConstant.ENABLE) {
                //处于启用时是不能删除
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        setmealMapper.DeleteSetMeal(ids);
        setMealDishMapper.DeleteSetMeal(ids);
    }

    @Override
    public SetmealVO getById(Long id) {
        SetmealVO setmealVO=setmealMapper.getById(id);
        List<SetmealDish> setmealDish=setMealDishMapper.getById(id);
        setmealVO.setSetmealDishes(setmealDish);
        return setmealVO;
    }

    @Override
    @CacheEvict(cacheNames = "SETMEAL", allEntries = true)
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.update(setmeal);
        Long setmealId = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealId);
        });
        setMealDishMapper.deleteSetMeal(setmealId);
        setMealDishMapper.insertBatch(setmealDishes);
    }

    @Override
    public void modifyStatus(Long id, Integer status) {
        Setmeal setmeal= new Setmeal();
        setmeal.setId(id);
        setmeal.setStatus(status);
        setmeal.setUpdateTime(LocalDateTime.now());
        setmeal.setUpdateUser(BaseContext.getCurrentId());
       Boolean isFlag =false;
       List<Long> dishId=setMealDishMapper.select(setmeal);
        for (Long aLong : dishId) {
            Integer dishstatus=dishMapper.select(aLong);
            if(dishstatus==StatusConstant.DISABLE){
                throw new SetmealEnableFailedException("有停售菜品，套餐无法上架");
            }else if(dishstatus==StatusConstant.ENABLE){
                isFlag = true;
            }
        }
        if(isFlag=true){
            setmealMapper.update(setmeal);
        }
    }
}
